package com.lijianan.utils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;

public class DateSplit {

    public static String spiltAll(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return dateFormat.format(date);
    }

    public static String spiltYMD(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        return dateFormat.format(date);
    }

    public static String spiltHMS(Date date) {
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

        return timeFormat.format(date);
    }

    public static Date changeYMD(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // adjust pattern to match your string format
        try {
            return format.parse(time);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static Date changeHMS(String time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); // adjust pattern to match your string format
        try {
            return format.parse(time);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static Date spilt(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // adjust pattern to match your string format
        try {
            return format.parse(time);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static Date mergeDateTime(Date datePart, Date timePart) {
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(datePart);

        Calendar timeCalendar = Calendar.getInstance();
        timeCalendar.setTime(timePart);

        dateCalendar.set(Calendar.HOUR_OF_DAY, timeCalendar.get(Calendar.HOUR_OF_DAY));
        dateCalendar.set(Calendar.MINUTE, timeCalendar.get(Calendar.MINUTE));
        dateCalendar.set(Calendar.SECOND, timeCalendar.get(Calendar.SECOND));
        dateCalendar.set(Calendar.MILLISECOND, timeCalendar.get(Calendar.MILLISECOND));

        return dateCalendar.getTime();
    }
}
